import { createRouter, createWebHistory } from 'vue-router'


const routes = [
  // 示例路由

  {
    path: '/',
    name: 'index',
    component: () => import('../views/IndexView.vue')
  },
   {
    path: '/buyCar',
    name: 'buyCar',
    component: () => import('../views/BuyCar.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/LoginView.vue')
  },
  {
    path: '/regist',
    name: 'regist',
    component: () => import('@/components/Regist.vue')
  },

  {
    path: '/newsDetail',
    name: 'newsDetail',
    component: () => import('@/views/NewsDetailView.vue')
  }, {
    path: '/orderDetailView',
    name: 'orderDetailView',
    component: () => import('@/views/OrderDetailView.vue')
  },
  {
    path: '/searchProduct',
    name: 'searchProduct',
    component: () => import('@/views/SearchProduct.vue')
  },
  {
    path: '/productDetail',
    name: 'productDetail',
    component: () => import('@/views/ProductDetailView.vue')
  }, 
  {
    path: '/buyCar_Two',
    name: 'buyCar_Two',
    component: () => import('@/views/BuyCar_Two.vue')
  },
  {
    path: '/buyCar_Three',
    name: 'buyCar_Three',
    component: () => import('@/views/BuyCar_Three.vue')
  },
  {
    path: '/return',
    name: 'return',
    component: () => import('@/views/Return.vue')
  },
  {
    path: '/management',
    name: 'management',
    component: () => import('@/views/ManagementView.vue'),
    children: [
      {
        path: '/allOrder',
        name: 'allOrder',
        component: () => import('@/views/AllOrder.vue')
      },
      {
        path: '/userInfo',
        name: 'userInfo',
        component: () => import('@/views/UserInfo.vue')
      },
      {
        path: '/userList',
        name: 'userList',
        component: () => import('@/views/UserList.vue')
      },
      {
        path: '/categotyManage',
        name: 'categotyManage',
        component: () => import('@/views/CategotyManage.vue')
      },
      {
        path: '/productManage',
        name: 'productManage',
        component: () => import('@/views/ProductManage.vue')
      },
      {
        path: '/addProduct',
        name: 'addProduct',
        component: () => import('@/views/AddProduct.vue')
      },
      {
        path: '/newsList',
        name: 'newsList',
        component: () => import('@/views/NewsList.vue')
      }, {
        path: '/addNews',
        name: 'addNews',
        component: () => import('@/views/AddNewsView.vue')
      }, {
        path: '/updateNews',
        name: 'updateNews',
        component: () => import('@/views/UpdateNewsView.vue')
      }, {
        path: '/addressList',
        name: 'addressList',
        component: () => import('@/views/AddressList.vue')
      },
      {
        path: '/addAddress',
        name: 'addAddress',
        component: () => import('@/views/AddAddress.vue')
      },
      {
        path: '/updateAddress',
        name: 'updateAddress',
        component: () => import('@/views/updateAddress.vue')
      },
      {
        path: '/updateProduct',
        name: 'updateProduct',
        component: () => import('@/views/UpdateProduct.vue')
      },
      {
        path: '/collectProduct',
        name: 'collectProduct',
        component: () => import('@/views/CollectProduct.vue')
      }
    ]
  },

]

const router = createRouter({
  history: createWebHistory(),
  routes,
})

// 全局后置守卫
router.afterEach((to, from) => {
  // 触发一个自定义事件，通知Header组件检查登录状态
  window.dispatchEvent(new Event('route-changed'))
})

export default router